#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from flask import Response
from flask_restful import Resource
from src.formats.MapServiceManager import mapServiceManager

sys.path.append("../..")

# 全局变量
# mapServiceManager = MapServiceManager.mapServiceManager


class TMSRoot(Resource):
    def get(self, servicename):
        resp = ''
        try:
            res = mapServiceManager.serviceList[servicename][
                'tilesManager'].TMSRootXML

            resp = Response(res, status=200, mimetype='text/xml')
            resp.headers['content-type'] = 'text/xml'
        except Exception:
            resp = Response('ServiceName Not Found',
                            status=404,
                            mimetype='text/javascript')
        finally:
            return resp


class TMSService(Resource):
    def get(self, servicename):
        resp = ''
        try:
            res = mapServiceManager.serviceList[servicename][
                'tilesManager'].TMSBasemapXML

            resp = Response(res, status=200, mimetype='text/xml')
            resp.headers['content-type'] = 'text/xml'
        except Exception:
            resp = Response('ServiceName Not Found',
                            status=404,
                            mimetype='text/javascript')
        finally:
            return resp


class TMSBasemap(Resource):
    def get(self, servicename):
        resp = ''
        try:
            res = mapServiceManager.serviceList[servicename][
                'tilesManager'].TMSTilemapserviceXML

            resp = Response(res, status=200, mimetype='text/xml')
            resp.headers['content-type'] = 'text/xml'
        except Exception:
            resp = Response('ServiceName Not Found',
                            status=404,
                            mimetype='text/javascript')
        finally:
            return resp
